RegexHTML = 
{
   currentContent : "",
   currentGroup : 0,
   currentResult : null,
   lastSearchPos : 0,
   
   init : function()
   {
      prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);   
      var matchesnewline = prefs.getBoolPref("extensions.regex_html.dotmatchesnewline");
      var ignorecase = prefs.getBoolPref("extensions.regex_html.ignorecase");
      var testontyping = prefs.getBoolPref("extensions.regex_html.testontyping");            
      
      document.getElementById('matchesnewline').checked  = matchesnewline;
      document.getElementById('ignorecase').checked  = ignorecase;
      document.getElementById('testontyping').checked  = testontyping;            
   
   },
   
   initViewSource : function()
   {
      prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);   
      var matchesnewline = prefs.getBoolPref("extensions.regex_html.dotmatchesnewline");
      var ignorecase = prefs.getBoolPref("extensions.regex_html.ignorecase");
      var testontyping = prefs.getBoolPref("extensions.regex_html.testontyping");            
      var showonviewsource = prefs.getBoolPref("extensions.regex_html.showonviewsource");            

      document.getElementById('regex_html_source_box').hidden = !showonviewsource;
      document.getElementById('regex_html-splitter').hidden = !showonviewsource;
      
      document.getElementById('menu_regex_html_1').setAttribute("checked", showonviewsource);
        
      document.getElementById('matchesnewline').checked  = matchesnewline;
      document.getElementById('ignorecase').checked  = ignorecase;
      document.getElementById('testontyping').checked  = testontyping;

      onLoadViewSource();
   },
   
   displayNullResult : function()
   {
      var resultBox = document.getElementById('regex_html_result');      
      resultBox.value = "";
      var menu_popup = document.getElementById('regex_html_menu_list');
      menu_popup.removeAllItems();
   },
      
   executePatternFromSource : function(force)
   {
      RegexHTML.currentContent = content.document.body.textContent;
      RegexHTML.executePattern(force);
   },
      
   executePattern : function(force)
   {
      var matchesnewline = prefs.getBoolPref("extensions.regex_html.dotmatchesnewline");
      var ignorecase = prefs.getBoolPref("extensions.regex_html.ignorecase");
      var testontyping = prefs.getBoolPref("extensions.regex_html.testontyping");            
      
      if (!testontyping && !force)
      {
         return;
      }

      var pattern = document.getElementById('regex_html_pattern').value;
      var resultBox = document.getElementById('regex_html_result');
      var c = RegexHTML.currentContent;

      var flag = "g";

      if (ignorecase)
      {
         flag += "i";
      }
      
      if (matchesnewline)
      {
         c = c.replace(/\n/g,"");
         c = c.replace(/\r/g,"");
      }

      if (c=="" || pattern =="")
      {
         return;
      }
      
      try{
         var regex = new RegExp(pattern,flag);
      }
      catch (e)
      {
         RegexHTML.displayNullResult();
         return;
      }
      
      var myArray;
      var i = 0;      
            
      var maxsize = 0;
      currentResult = new Array();
      
      while ((myArray = regex.exec(c)) != null)
      {
         currentResult.push(myArray);
         
         if (myArray.length > maxsize)
         {
            maxsize = myArray.length;
         }
                  
         i++;
         if (i > 50)
         {
            break;
         }
      }         
      
      RegexHTML.currentResult = currentResult;
      if (i==0)
      {
         resultBox.value = "No match found!";
      }
      else
      {
         var menu_popup = document.getElementById('regex_html_menu_list');
         menu_popup.removeAllItems();
         for (var j=0;j<maxsize;j++)
         {
            if (j == 0)
            {
               menu_popup.appendItem("Display full matched text",j,"");
            }
            else
            {
               menu_popup.appendItem("Display group "+j,j,"");            
            }
         }
      }

      if (RegexHTML.currentGroup > maxsize-1)
      {
         RegexHTML.currentGroup = 0;
         var menu_popup = document.getElementById('regex_html_menu_list');
         menu_popup.selectedIndex = 0;
      }
      else
      {
         menu_popup.selectedIndex = RegexHTML.currentGroup;
         RegexHTML.changeGroup();      
      }
   },

   updatePref : function()
   {
      var matchesnewline = document.getElementById('matchesnewline').checked;
      var ignorecase = document.getElementById('ignorecase').checked;
      var testontyping = document.getElementById('testontyping').checked;            

      prefs.setBoolPref("extensions.regex_html.dotmatchesnewline", matchesnewline);
      prefs.setBoolPref("extensions.regex_html.ignorecase", ignorecase);            
      prefs.setBoolPref("extensions.regex_html.testontyping", testontyping);
      RegexHTML.executePattern();
   },

   updateContentVar : function()
   {
      RegexHTML.currentContent = document.getElementById('regex_html_content').value;
      RegexHTML.executePattern(false);
      RegexHTML.lastSearchPos = 0;
   },

   selectGroup : function()
   {
      var menu_popup = document.getElementById('regex_html_menu_list');
      RegexHTML.currentGroup = menu_popup.value;
      RegexHTML.changeGroup();
   },
   
   changeGroup : function()
   {
      var currentResult = RegexHTML.currentResult;
      var resultBox = document.getElementById('regex_html_result');      
      var group = RegexHTML.currentGroup;
      resultBox.value = "";
      if (!currentResult) return;

      for (i=0; i < currentResult.length; i++)
      {
         var msg = "["+i+"] : " + currentResult[i][group] + "\n";         
         resultBox.value += msg;            
      }            
      resultBox.value += "\n\n\n";
   },
   
   doSearch : function()
   {  
      var box_content = document.getElementById('regex_html_content');
      var search_content = document.getElementById('regex_html_search_content').value;
      if (search_content == "")
      {
         return;
      }
      var content = box_content.value.toLowerCase().substring(RegexHTML.lastSearchPos);

      var start = content.indexOf(search_content);
      var end;
      if (start == -1)
      {
         start = box_content.value.toLowerCase().indexOf(search_content);
         end = start + search_content.length;
      }
      else
      {
         start = start + RegexHTML.lastSearchPos;
         end = start + search_content.length;
      }
   
      if (start == -1)   
      {
         RegexHTML.lastSearchPos = 0;
      }
      else
      {
         RegexHTML.lastSearchPos = end;         
         box_content.select();
//         box_content.setSelectionRange(start,start+1);
//         var ev = document.createEvent ('KeyEvent');
//         ev.initKeyEvent('keypress', true, true, window,false, false, false, false, 0,box_content.value.charCodeAt(start));
//         box_content.dispatchEvent(ev); // causes the scrolling    
         box_content.setSelectionRange( start,end );
         var xpcomInterface =  box_content.boxObject.QueryInterface(Components.interfaces.nsIScrollBoxObject);
             alert(xpcomInterface);
         xpcomInterface.scrollToIndex(start);         
      }      
   },
   
   toggleRegexHTML : function()
   {
      var b = document.getElementById('regex_html_source_box').hidden;
      document.getElementById('regex_html_source_box').hidden = !b;
      document.getElementById('regex_html-splitter').hidden = !b;    
      prefs.setBoolPref("extensions.regex_html.showonviewsource", b);
   },

   copyToClipboard : function()
   {

      gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].  
      getService(Components.interfaces.nsIClipboardHelper);  

      var copiedResult = "";
      var currentResult = RegexHTML.currentResult;
      var group = RegexHTML.currentGroup;
      if (!currentResult) return;

      for (i=0; i < currentResult.length; i++)
      {
         copiedResult += currentResult[i][group] + "\n";
      }
      gClipboardHelper.copyString(copiedResult);  
   }   
}
